﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Upgrade;
using Microsoft.SharePoint.Utilities;


namespace ProjectHT.YKDGVB2012.Core.FeatureAction
{
    public static class CreateList
    {
        /// <summary>
        /// create main list  
        /// </summary>
        /// <param name="sweb"></param>
        /// <returns>obj List</returns>
        public static void MainList(SPWeb sweb)
        {
            sweb.AllowUnsafeUpdates = true;
            SPList splist = null;
            try
            {
                SPListCollection listcoll = sweb.Lists;
                foreach (SPList list in listcoll)
                {
                    if (!list.Title.ToLower().Equals(Common.MainList.MAINLIST.ToLower()))
                    {
                        Guid listId = listcoll.Add(Common.MainList.MAINLIST, Common.MainList.MAINLISTDESCRIPTION, SPListTemplateType.CustomGrid);

                        var title = (SPFieldText)listcoll[listId].Fields[Common.MainList.FIELDTITLE];
                        title.Title = Common.MainList.FIELDTITLE;
                        title.StaticName = Common.MainList.FIELDTITLE;
                        title.Update();

                        listcoll[listId].Fields.Add(Common.MainList.FIELDDESCRIPTION, SPFieldType.Text, false);
                        var description = (SPFieldText)listcoll[listId].Fields[Common.MainList.FIELDDESCRIPTION];
                        description.Title = Common.MainList.FIELDDESCRIPTION;
                        description.StaticName = Common.MainList.FIELDDESCRIPTION;
                        description.Update();


                        listcoll[listId].Fields.Add(Common.MainList.FIELDFULLCONTENT, SPFieldType.Note, false);
                        var fullcontent = (SPFieldMultiLineText)listcoll[listId].Fields[Common.MainList.FIELDFULLCONTENT];
                        fullcontent.Title = Common.MainList.FIELDFULLCONTENT;
                        fullcontent.RichTextMode = SPRichTextMode.FullHtml;
                        fullcontent.StaticName = Common.MainList.FIELDFULLCONTENT;
                        fullcontent.Update();

                        listcoll[listId].Fields.Add(Common.MainList.FIELDSTATUS, SPFieldType.Boolean, false);
                        var status = (SPFieldBoolean)listcoll[listId].Fields[Common.MainList.FIELDSTATUS];
                        status.Title = Common.MainList.FIELDSTATUS;
                        status.StaticName = Common.MainList.FIELDSTATUS;
                        status.Update();

                        listcoll[listId].Fields.Add(Common.MainList.FIELDEXPIRE, SPFieldType.Boolean, false);
                        var expires = (SPFieldBoolean)listcoll[listId].Fields[Common.MainList.FIELDEXPIRE];
                        expires.Title = Common.MainList.FIELDEXPIRE;
                        expires.StaticName = Common.MainList.FIELDEXPIRE;
                        expires.Update();

                        listcoll[listId].Fields.Add(Common.MainList.FIELDEXPIREDATE, SPFieldType.DateTime, false);
                        var expiresDate = (SPFieldDateTime)listcoll[listId].Fields[Common.MainList.FIELDEXPIREDATE];
                        expiresDate.Title = Common.MainList.FIELDEXPIRE;
                        expiresDate.StaticName = Common.MainList.FIELDEXPIRE;
                        expiresDate.DisplayFormat = SPDateTimeFieldFormatType.DateTime;
                        expiresDate.Update();

                        listcoll[listId].Hidden = true;
                        listcoll[listId].EnableAttachments = true;
                        listcoll[listId].Update();

                        var viewcoll = listcoll[listId].Views;
                        var defaultsguid = listcoll[listId].Views[0].ID;
                        viewcoll.Delete(defaultsguid);
                        SPSecurity.RunWithElevatedPrivileges(delegate
                        {
                            listcoll[listId].BreakRoleInheritance(false);
                            var group = sweb.AssociatedOwnerGroup;
                            group.AddUser(sweb.CurrentUser);
                            var roleassing = new SPRoleAssignment(group);
                            var roledefind = sweb.RoleDefinitions["Full Control"];
                            roleassing.RoleDefinitionBindings.Add(roledefind);
                            listcoll[listId].RoleAssignments.Add(roleassing);
                            listcoll[listId].AnonymousPermMask64 |= SPBasePermissions.EmptyMask;
                            listcoll[listId].AllowEveryoneViewItems = false;
                            listcoll[listId].Update();
                        });
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// Create Comment list
        /// </summary>
        /// <param name="sweb"></param>
        /// <returns></returns>
        public static void CommentList(SPWeb sweb)
        {
            sweb.AllowUnsafeUpdates = true;
            SPList rtlist = null;
            try
            {
                SPListCollection listcoll = sweb.Lists;
                foreach (SPList list in listcoll)
                {
                    if (!list.Title.ToLower().Equals(Common.CommentList.COMMENTLIST.ToLower()))
                    {
                        Guid listId = listcoll.Add(Common.CommentList.COMMENTLIST, Common.CommentList.COMMENTDESCRIPTION, SPListTemplateType.CustomGrid);

                        var title = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDTITLE];
                        title.Title = Common.CommentList.FIELDTITLE;
                        title.StaticName = Common.CommentList.FIELDTITLE;
                        title.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDFULLNAME, SPFieldType.Text, false);
                        var fullname = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDFULLNAME];
                        fullname.Title = Common.CommentList.FIELDFULLNAME;
                        fullname.StaticName = Common.CommentList.FIELDFULLNAME;
                        fullname.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDMAINID, SPFieldType.Text, false);
                        var mainid = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDMAINID];
                        mainid.Title = Common.CommentList.FIELDMAINID;
                        mainid.StaticName = Common.CommentList.FIELDMAINID;
                        mainid.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDPHONE, SPFieldType.Text, false);
                        var phone = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDPHONE];
                        phone.Title = Common.CommentList.FIELDPHONE;
                        phone.StaticName = Common.CommentList.FIELDPHONE;
                        phone.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDEMAIL, SPFieldType.Text, false);
                        var email = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDEMAIL];
                        email.Title = Common.CommentList.FIELDEMAIL;
                        email.StaticName = Common.CommentList.FIELDEMAIL;
                        email.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDADDRESS, SPFieldType.Note, false);
                        var Address = (SPFieldMultiLineText)listcoll[listId].Fields[Common.CommentList.FIELDADDRESS];
                        Address.Title = Common.CommentList.FIELDADDRESS;
                        Address.RichTextMode = SPRichTextMode.Compatible;
                        Address.StaticName = Common.CommentList.FIELDADDRESS;
                        Address.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDCONTENTS, SPFieldType.Note, false);
                        var contents = (SPFieldMultiLineText)listcoll[listId].Fields[Common.CommentList.FIELDCONTENTS];
                        contents.Title = Common.CommentList.FIELDCONTENTS;
                        contents.RichTextMode = SPRichTextMode.Compatible;
                        contents.StaticName = Common.CommentList.FIELDCONTENTS;
                        contents.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDFAX, SPFieldType.Text, false);
                        var fax = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDFAX];
                        fax.Title = Common.CommentList.FIELDFAX;
                        fax.StaticName = Common.CommentList.FIELDFAX;
                        fax.Update();


                        listcoll[listId].Fields.Add(Common.CommentList.FIELDSTATUS, SPFieldType.Boolean, false);
                        var status = (SPFieldBoolean)listcoll[listId].Fields[Common.CommentList.FIELDSTATUS];
                        status.Title = Common.CommentList.FIELDSTATUS;
                        status.StaticName = Common.CommentList.FIELDSTATUS;
                        status.Update();

                        listcoll[listId].AllowDeletion = false;
                        listcoll[listId].AllowEveryoneViewItems = false;
                        listcoll[listId].EnableAttachments = true;
                        listcoll[listId].Update();

                        var viewcoll = listcoll[listId].Views;
                        var defaultsguid = listcoll[listId].Views[0].ID;
                        viewcoll.Delete(defaultsguid);


                        SPSecurity.RunWithElevatedPrivileges(delegate
                        {
                            listcoll[listId].BreakRoleInheritance(false);
                            var group = sweb.AssociatedOwnerGroup;
                            group.AddUser(sweb.CurrentUser);
                            var roleassing = new SPRoleAssignment(group);
                            var roledefind = sweb.RoleDefinitions["Full Control"];
                            roleassing.RoleDefinitionBindings.Add(roledefind);
                            listcoll[listId].RoleAssignments.Add(roleassing);
                            listcoll[listId].AnonymousPermMask64 |= SPBasePermissions.EmptyMask;
                            listcoll[listId].AllowEveryoneViewItems = false;
                            listcoll[listId].Update();
                        });
                    }

                }
            }
            catch (SPException ex)
            {

            }
        }


        /// <summary>
        /// List main of Tham dinh du an
        /// </summary>
        /// <param name="sweb"></param>
        public static void Projectlist(SPWeb sweb)
        {
            sweb.AllowUnsafeUpdates = true;
            SPList rtlist = null;
            try
            {
                SPListCollection listcoll = sweb.Lists;
                foreach (SPList list in listcoll)
                {
                    if (!list.Title.ToLower().Equals(Common.ProjectList.PROLIST.ToLower()))
                    {
                        Guid listId = listcoll.Add(Common.ProjectList.PROLIST, Common.ProjectList.PROLISTDESCRIPTION, SPListTemplateType.CustomGrid);

                        var title = (SPFieldText)listcoll[listId].Fields[Common.ProjectList.FIELDTITLE];
                        title.Title = Common.ProjectList.FIELDTITLE;
                        title.StaticName = Common.ProjectList.FIELDTITLE;
                        title.Update();

                        listcoll[listId].Fields.Add(Common.ProjectList.FIELDFULLNAME, SPFieldType.Text, false);
                        var fullname = (SPFieldText)listcoll[listId].Fields[Common.ProjectList.FIELDFULLNAME];
                        fullname.Title = Common.ProjectList.FIELDFULLNAME;
                        fullname.StaticName = Common.ProjectList.FIELDFULLNAME;
                        fullname.Update();

                        listcoll[listId].Fields.Add(Common.ProjectList.FIELDREGCODE, SPFieldType.Text, false);
                        var mainid = (SPFieldText)listcoll[listId].Fields[Common.ProjectList.FIELDREGCODE];
                        mainid.Title = Common.ProjectList.FIELDREGCODE;
                        mainid.StaticName = Common.ProjectList.FIELDREGCODE;
                        mainid.Update();

                        listcoll[listId].Fields.Add(Common.ProjectList.FIELDMOBIPHONE, SPFieldType.Text, false);
                        var phone = (SPFieldText)listcoll[listId].Fields[Common.ProjectList.FIELDMOBIPHONE];
                        phone.Title = Common.ProjectList.FIELDMOBIPHONE;
                        phone.StaticName = Common.ProjectList.FIELDMOBIPHONE;
                        phone.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDEMAIL, SPFieldType.Text, false);
                        var email = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDEMAIL];
                        email.Title = Common.CommentList.FIELDEMAIL;
                        email.StaticName = Common.CommentList.FIELDEMAIL;
                        email.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDADDRESS, SPFieldType.Note, false);
                        var Address = (SPFieldMultiLineText)listcoll[listId].Fields[Common.CommentList.FIELDADDRESS];
                        Address.Title = Common.CommentList.FIELDADDRESS;
                        Address.RichTextMode = SPRichTextMode.Compatible;
                        Address.StaticName = Common.CommentList.FIELDADDRESS;
                        Address.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDCONTENTS, SPFieldType.Note, false);
                        var contents = (SPFieldMultiLineText)listcoll[listId].Fields[Common.CommentList.FIELDCONTENTS];
                        contents.Title = Common.CommentList.FIELDCONTENTS;
                        contents.RichTextMode = SPRichTextMode.Compatible;
                        contents.StaticName = Common.CommentList.FIELDCONTENTS;
                        contents.Update();

                        listcoll[listId].Fields.Add(Common.CommentList.FIELDFAX, SPFieldType.Text, false);
                        var fax = (SPFieldText)listcoll[listId].Fields[Common.CommentList.FIELDFAX];
                        fax.Title = Common.CommentList.FIELDFAX;
                        fax.StaticName = Common.CommentList.FIELDFAX;
                        fax.Update();


                        listcoll[listId].Fields.Add(Common.CommentList.FIELDSTATUS, SPFieldType.Boolean, false);
                        var status = (SPFieldBoolean)listcoll[listId].Fields[Common.CommentList.FIELDSTATUS];
                        status.Title = Common.CommentList.FIELDSTATUS;
                        status.StaticName = Common.CommentList.FIELDSTATUS;
                        status.Update();

                        listcoll[listId].AllowDeletion = false;
                        listcoll[listId].AllowEveryoneViewItems = false;
                        listcoll[listId].EnableAttachments = true;
                        listcoll[listId].Update();

                        var viewcoll = listcoll[listId].Views;
                        var defaultsguid = listcoll[listId].Views[0].ID;
                        viewcoll.Delete(defaultsguid);


                        SPSecurity.RunWithElevatedPrivileges(delegate
                        {
                            listcoll[listId].BreakRoleInheritance(false);
                            var group = sweb.AssociatedOwnerGroup;
                            group.AddUser(sweb.CurrentUser);
                            var roleassing = new SPRoleAssignment(group);
                            var roledefind = sweb.RoleDefinitions["Full Control"];
                            roleassing.RoleDefinitionBindings.Add(roledefind);
                            listcoll[listId].RoleAssignments.Add(roleassing);
                            listcoll[listId].AnonymousPermMask64 |= SPBasePermissions.EmptyMask;
                            listcoll[listId].AllowEveryoneViewItems = false;
                            listcoll[listId].Update();
                        });
                    }

                }
            }
            catch (SPException ex)
            {

            }



            

        }
    }
}
